//
//  Created by Huly-Gun Megatron on 10/3/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//


#import "game.h"
#include "shashka.h"
//#import "menu.h"
//#import "gameover.h"
#import "vars.h"
#import "HiScore.h"
#import <iostream>


void GameStage::Load()
{

    controller = (ViewController*)curController;
    controller.pauseBtn.hidden = false;

    //gameProhod = 1;
    
    defCameraZoom = 0.6;
    maxCameraZoom = 0.45;
    
    if (IS_IPHONE_5)
    {
        defCameraZoom = 0.4;
        maxCameraZoom = 0.3;
    }
    
	drawScore(true, 0);
	camera=xCreateCamera(0);
	xPositionEntity(camera, 0.0f, 0.0f, 25.0f, false);
	xCameraZoom(camera, defCameraZoom);//1.6
	
	light=xCreateLight(3, camera);
    xRotateEntity(light, 0, 0, 90, 1);
	
	pole=xLoadMesh("desk.b3d",0 );
	xScaleMesh(pole, .14, .14, .14);
    xRotateEntity(pole, 90, 0, 0, 1);
	xShowEntity(pole);
	xUpdateNormals(pole);
	
	pick_pole=xCreateCube(0);
	xScaleMesh(pick_pole, 39, 39, 0.01);
	xEntityPickMode(pick_pole,2);
	xEntityAlpha(pick_pole, 0);
	
    white=xLoadMesh("white.b3d", 0);
    black=xLoadMesh("black.b3d", 0);
    img_color=xLoadAnimImage("b_w_3.png",64,32,0,2);	
    img_ready=xLoadAnimImage("ready.png", 90, 45, 0, 2);
    img_win=xLoadAnimImage("BLACK_WHITE.png", 300, 80, 0, 2);


	xScaleMesh(white, 0.023, 0.0075, 0.023);	
	xScaleMesh(black, 0.023, 0.0075, 0.023);
	
	xHideEntity(black);
	xHideEntity(white);	
	
	arrow=xLoadMesh("arrow.b3d",0);
	xHideEntity(arrow);
	xUpdateNormals(arrow);
	
	score100=xLoadMesh("100.b3d", 0);
	xUpdateNormals(score100);
	xHideEntity(score100);	
	
	score200=xLoadMesh("300.b3d", 0);
	xUpdateNormals(score200);	
	xHideEntity(score200);
	
	exellent=xLoadMesh("500.b3d", 0);
	xUpdateNormals(exellent);
	xHideEntity(exellent);
	
	quad=xLoadMesh("1000.b3d", 0);
	xUpdateNormals(quad);
	xHideEntity(quad);
	
	xPositionEntity(score100, 0, 1, 1.1, 1);
	xPositionEntity(score200, 0, 1, 1.1, 1);
	xPositionEntity(exellent, 0, 1, 1.1, 1);
	xPositionEntity(quad, 0, 1, 1.1, 1);
	
	
	sound_s=xLoadSound("udar01.m4a");
	snd_pick=xLoadSound("pick.m4a");
	snd_pick2=xLoadSound("pick2.m4a");
	snd_shoot=xLoadSound("shot.m4a");
	
	if (soundPresent==true){
		xSoundVolume(sound_s, 0.5);
		xSoundVolume(snd_pick, 1);
		xSoundVolume(snd_pick2, 1);
		xSoundVolume(snd_shoot, 0.7);
	}else{
		xSoundVolume(sound_s, 0.0);
		xSoundVolume(snd_pick, 0.0);
		xSoundVolume(snd_pick2, 0.0);
		xSoundVolume(snd_shoot, 0.0);
	}
	
	
	CreateShashka();
	
	activeColor = sht_white;
	prevBlack = SH_COUNT, prevWhite = SH_COUNT;

	//activeShashka = NULL;
	gameState = gsReadyForTouch;
	
	g_timer=0;
	cnt_remove_white=0;
	cnt_remove_black=0;
	scoreBlack = scoreWhite = 0;
	camera_zoom = 0.0;
		
	xCollisions(1,1,1,2);
	
	// load fonts
//	_mainFont             = xLoadFont("mainfont");
	
	img_gui=xLoadImage("GUI.png");
	img_dig_s=xLoadAnimImage("dig2.png",13, 24, 0, 10);
	
    NSLog(@"avail mem: %f", xGetAvailPhysMem());
    
	// set stage as active
	MakeActive();
}

void GameStage::Update()
{
	if (gameState == gsWin)
	{
		g_timer=g_timer + 1;
        
		//if (xCountTouches() > 0){
		//	g_timer = 70;
		//	xPlaySound(snd_pick);
		//}
		
		if (g_timer==70){
        
			//ScoreStage *stage = new ScoreStage;
            //stage->Load();
            //return;
            //exitGame();
            
            global_pause = false;
            [controller showCurrentScore];
		}
		move_Shashka();
		
	}
	else
	
	if (activeColor == sht_white){
		if (check_Touch()){
			gameState = gsShashkaMove;
			move_Shashka();
		}
	}
	else
		if (activeColor == sht_black)
		{
			if (AI_timer==0){
				if ( gameState != gsShashkaMove){
					xPlaySound(snd_shoot);
					playAI();
				}
				gameState = gsShashkaMove;
				move_Shashka();
			}
			else AI_timer--;
		}	
	
	drawScore();

	removeShashka();
	
	xUpdateWorld(1.0);
    
}

void GameStage::Render()
{
	xRenderWorld(1.0);
	
	//xDrawImage(img_gui, 0, 0, 0);
	
	//xDrawImage(img_dig_s, 146, 9, level_cnt/10);
	//xDrawImage(img_dig_s, 162, 9, level_cnt%10);
	
	static int frame_t=0;

	if (gameState == gsHelp)
	{
		if (frame_t<440){ //590 570
			if (frame_t < 420)	frame_t=frame_t+5;
			else frame_t++;
		}
		else frame_t=0;
		xDrawImage(img_tutor, 87, 168, frame_t/50);
	}else frame_t=0;
	
	if (gameState==exit_game) xDrawImage(img_exit, 0,210 ,0);	
	
	int score1,score2,frame1l,frame1r,frame2l,frame2r;
	
	score1 = g_score1 = scoreWhite*100;
	score2 = g_score2 = scoreBlack*100;
	
	frame1r=(score1/100)%10;
	frame1l=score1/1000;
	
	frame2r=(score2/100)%10;
	frame2l=score2/1000;
	
	xDrawImage(img_dig_s, 8, 16, frame1l);
	xDrawImage(img_dig_s, 22, 16, frame1r);
	xDrawImage(img_dig_s, 38, 16, 0);
	xDrawImage(img_dig_s, 53, 16, 0);
	
	xDrawImage(img_dig_s, 256, 16, frame2l);	
	xDrawImage(img_dig_s, 271, 16, frame2r);
	xDrawImage(img_dig_s, 286, 16, 0);
	xDrawImage(img_dig_s, 301, 16, 0);
	
	xDrawImage(img_color, 73, 7, 1);
	xDrawImage(img_color, 187, 7, 0);
	
	if (activeColor == sht_white){
		xDrawImage(img_ready, 5, 429, 0);
	}else{
		xDrawImage(img_ready, 5, 429, 1);
	}
	
	if (g_timer>0){
		if (lastGameBlackCount==0){
			xDrawImage(img_win, 10,180,1);
		}else {
			xDrawImage(img_win, 10, 180,0);
		}
	}

/*	// draw text information
	xSetFont(_mainFont);
	char buff[256];
	//sprintf(buff, "FPS: %i \t %i \n%i", prevBlack, prevWhite, activeShashka);//xFPSCounter(), dxxv);
	sprintf(buff, "b: %i\n w: %i",cnt_remove_black,cnt_remove_white);
	xText(5, 5, buff, false, false);
	
*/
	
	xFlip();	
}

void GameStage::Unload()
{

    xCls();
    xFlip();

	std::vector<Shashka*>::iterator i;
	for (i = shashka_l.begin(); i != shashka_l.end(); i++){
		
		delete (*i);
		*i = NULL;
	}
	shashka_l.clear();

	xFreeEntity(black);
	xFreeEntity(white);
	black = white = NULL;
	
	
	xFreeImage(img_win);
	xFreeImage(img_gui);
	xFreeImage(img_dig_s);
	xFreeImage(img_ready);
	xFreeImage(img_color);

	img_win = img_gui = img_dig_s = img_ready = img_color = img_tutor = NULL;
		
	xFreeEntity(pole);
	
    xFreeEntity(light);
	xFreeEntity(camera);
	xFreeEntity(pick_pole);
	xFreeEntity(arrow);
	xFreeEntity(score100);
	xFreeEntity(score200);
	xFreeEntity(exellent);
	xFreeEntity(quad);
	pole = camera = light = pick_pole = arrow = NULL;
	tex_pole = NULL;

	
//	xFreeFont(_mainFont);
	xFreeSound(sound_s);
//	xFreeSound(sound_r);
	xFreeSound(snd_pick);
	xFreeSound(snd_pick2);
	xFreeSound(snd_shoot);
	sound_s = snd_pick = snd_pick2 = snd_shoot = NULL;
	
	xFlushTouches();
}
/////////////////////////////

//========================================================
// Расстановка шашек
void GameStage::CreateShashka(const int prohod, const int step, const int color)
{
	int antibonus=0; 
	int s = (color == sht_white ? 1 : -1);
	float y[SH_COUNT];// = (s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
	float x[SH_COUNT];
	switch(prohod)
	{
		case 1:
		{
			if (step>2 & step<5) antibonus=1;	
			if (step>4) antibonus=2;
//			float y=0;
//			if (color == sht_white) y = BRD_MIN + CELL_DIM*step;
//			if (color == sht_black) y = BRD_MAX - CELL_DIM*step;
			for(int i = antibonus; i< SH_COUNT-antibonus; i++)
			{
				x[i] = BRD_MIN + CELL_DIM*i;
				y[i] = (s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			}
			break;
		}
		case 2:
		{
			if (step>2) antibonus=1;	
									
			x[0]=x[4]=BRD_MIN;
			x[1]=x[5]=BRD_MIN+CELL_DIM*2;
			x[2]=x[6]=BRD_MAX-CELL_DIM*2;
			x[3]=x[7]=BRD_MAX;
			
			for(int i = antibonus; i< SH_COUNT-antibonus; i++){				
				if (i>3) y[i]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
				if (i<4) y[i]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;;
			}
			break;
		}
		case 3:
		{
			if (step>2) antibonus=1;
			
			x[2] = BRD_MIN; x[1] = BRD_MIN+CELL_DIM;
			x[0] = BRD_MIN+CELL_DIM*2.5; x[3] = BRD_MIN+CELL_DIM*0.5;
			x[4] = BRD_MAX-CELL_DIM*0.5; x[7] = BRD_MAX-CELL_DIM*2.5;
			x[6] = BRD_MAX-CELL_DIM;     x[5] = BRD_MAX;
			for(int i = antibonus; i< SH_COUNT-antibonus; i++)
			{
				y[i] = (s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
				if (i==3|i==4) y[i] += CELL_DIM*s; 
			}
			break;
		}
		case 4:
		{
			if (step>2) antibonus=1;
			
			y[0]=y[2]=y[4]=y[6]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			y[1]=y[3]=y[5]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			
			for(int i = antibonus; i< SH_COUNT-antibonus; i++)
			{
				x[i] = BRD_MIN + CELL_DIM*i;
			}
			break;
		}
		
		case 5:
		{		
			if (step>1 & step <3) antibonus=1;
			if (step>2) antibonus=2;
			
			x[0]=x[2]=BRD_MIN;
			x[1]=x[3]=BRD_MIN+CELL_DIM;		
			x[4]=x[6]=BRD_MAX-CELL_DIM;
			x[5]=x[7]=BRD_MAX;
			
			y[0]=y[1]=y[6]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			y[2]=y[3]=y[4]=y[5]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			
			break;
		}	
			
		case 6:
		{
			if (step>2) antibonus=1;
			
			x[0]=x[3]=BRD_MIN+CELL_DIM*1.5;
			x[1]=x[5]=BRD_MIN+CELL_DIM*3.5;		
			x[2]=x[7]=BRD_MAX-CELL_DIM*1.5;
			x[4]=BRD_MIN+CELL_DIM*2.5;
			x[6]=BRD_MAX-CELL_DIM*2.5;
			
			y[0]=y[1]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			y[4]=y[5]=y[6]=y[2]=y[3]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			
			break;
		}
			
		case 7:
		{
			if (step>2) antibonus=1;
			
			x[1]=BRD_MIN+CELL_DIM/2;
			x[0]=x[2]=BRD_MIN+CELL_DIM+CELL_DIM/2;		
			x[3]=BRD_MIN+CELL_DIM*2+CELL_DIM/2;
			x[4]=BRD_MAX-CELL_DIM*2-CELL_DIM/2;
			x[7]=x[6]=BRD_MAX-CELL_DIM-CELL_DIM/2;
			x[5]=BRD_MAX-CELL_DIM/2;
			
			y[1]=y[3]=y[4]=y[5]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM/2*s;
			y[0]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			y[2]=y[6]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			
			break;
		}	
			
		case 8:
		{
			if (step>2) antibonus=1;
			
			x[0]=x[1]=BRD_MIN;
			x[2]=BRD_MIN+CELL_DIM;		
			x[3]=BRD_MIN+CELL_DIM*3;
			x[4]=BRD_MAX-CELL_DIM*3;
			x[5]=BRD_MAX-CELL_DIM;
			x[6]=x[7]=BRD_MAX;
			
			y[0]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			y[1]=y[2]=y[3]=y[4]=y[5]=y[6]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
						
			break;
		}
			
		case 9:
		{
			if (step>2) antibonus=1;
			
			x[0]=BRD_MIN+CELL_DIM;
			x[1]=x[2]=BRD_MIN+CELL_DIM*2;		
			x[3]=BRD_MIN+CELL_DIM*3;
			x[4]=BRD_MAX-CELL_DIM*3;
			x[5]=x[6]=BRD_MAX-CELL_DIM*2;
			x[7]=BRD_MAX-CELL_DIM;
			
			y[0]=y[1]=y[6]=y[7]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s+CELL_DIM*s;
			y[2]=y[3]=y[4]=y[5]=(s == 1 ? BRD_MIN : BRD_MAX) + CELL_DIM*step*s;
			
			break;
		}
	}
	
	for(int i=antibonus; i< SH_COUNT-antibonus; i++){
		Shashka* sh = new Shashka(color, x[i], y[i], (color==sht_white?white:black));
		shashka_l.push_back(sh);
	}
}

void GameStage::CreateShashka()
{
	//gameProhod = 9;
	CreateShashka(gameProhod, blackStartLine, sht_black);
	CreateShashka(gameProhod, whiteStartLine, sht_white);
}

bool GameStage::check_Touch()
{
	static Shashka* sh = NULL; // активная шашка
	static bool move = false;
	static float x0, y0; 

	if (gameState == gsShashkaMove){
		xFlushTouches();
		return true;
	}
    
    if (global_pause) return true; //
	
	
	// игра
	if (gameState == gsReadyForTouch){
		for(int i=0; i < xCountTouches(); i++) {
			if(xTouchPhase(i) != 0) {
				float x_t = xTouchX(i), y_t = xTouchY(i);
                
                if (y_t < 50){
                    NSLog(@"debug win");
                    
                    gameState = gsWin;
                }
				
                //
				long ent = xCameraPick(camera,x_t,y_t);
				std::vector<Shashka*>::iterator i;
				for (i = shashka_l.begin(); i != shashka_l.end(); i++){
					if ((*i)->stype == sht_white)
						if(ent==(*i)->entity){
							xPlaySound(snd_pick2);
							sh = *i;
							//sh->activate();
							touched = true;
							move = false;
							gameState = gsTouched;
							x0 = xEntityX(sh->entity, 1);
							y0 = xEntityY(sh->entity, 1);
								
					}
				}
			}
		}
	}
    
	if (gameState == gsTouched)// & xCountTouches()==0)
		if (sh != NULL)
		{
			if(xTouchPhase(0) == 2)	xCameraPick(camera, xTouchX(0), xTouchY(0));
		
			sh->vx = x0 - xPickedX();
			sh->vy = y0 - xPickedY();
			sh->normalyzeMove(sqrt(sh->getRMove())*1.15);
			
			xScaleEntity(arrow, 0.03, 0.015*sh->getRMove(),0.03,0);
			xPositionEntity(arrow, x0, y0, 0.4, 1);
			xAlignToVector(arrow, sh->vx, sh->vy, 0, 2, 1.0);
					
			// Zoom
			if (xTouchX(0)>300 | xTouchX(0)<20 |xTouchY(0)>460 | xTouchY(0)<20) {
				if(sh->getRMove() > 1 & camera_zoom < maxCameraZoom) camera_zoom = camera_zoom + .015;  //0.45
			}
			else
				if(camera_zoom> 0.0) camera_zoom=camera_zoom - .015;
            
			xCameraZoom(camera, defCameraZoom - camera_zoom);
			
			if (sh->getRMove()>1.0)
			{
				sh->activate(true);
                xShowEntity(arrow);
				move = true;
			}
			else{
				xHideEntity(arrow);
			}
		}
	
    if (sh != NULL & xCountTouches()==0 & gameState == gsTouched){
		if (sh->getRMove() < 1){
			sh->activate(false);
			move = false;
			gameState = gsReadyForTouch;
			sh = NULL;
			touched = false;
		}else{
			xPlaySound(snd_shoot);
		}
    }
	//activeShashka = sh;
	
	return (move & (xCountTouches()==0) & (gameState == gsTouched| gameState == gsShashkaMove));
} 


void GameStage::move_Shashka()
{
	xHideEntity(arrow);	
	xCameraZoom(camera, defCameraZoom);
	camera_zoom = 0.0;
	
	// пинаем кого надо
	std::vector<Shashka*>::iterator i;
	for (i = shashka_l.begin(); i != shashka_l.end(); i++){
		float minDist = 100000;
		Shashka* kiked = NULL;
		
		if ((*i)->flag == 1)
		{
			Shashka* activeSh = *i;
			std::vector<Shashka*>::iterator itr ;
			for (itr = shashka_l.begin(); itr != shashka_l.end(); itr++){
				Shashka* sh = *itr;
				if (sh != activeSh){ // пропускаем текущую активную шашку 
					//float dist = activeSh->getDistance(sh);
					float dist=xEntityDistance(activeSh->entity, sh->entity);
					if(dist < minDist & activeSh != sh){
						minDist = dist;
						kiked = sh;
					}
				}
			}
			//dxx = minDist;
			if (kiked != NULL) if (kiked->flag != 1)
			if ( minDist < 2.4){ // столкновение
				
				xPlaySound(sound_s);
				
				//activeSh->activate(false);
				float x1 = activeSh->vx,// activeSh->getXMove(),
				y1 = activeSh->vy,//activeSh->getYMove(),
				x2 = xEntityX(kiked->entity,true) - xEntityX(activeSh->entity,true),
				y2 = xEntityY(kiked->entity,true) - xEntityY(activeSh->entity,true);				
				
				float cos_alpha = (x1*x2+y1*y2)/sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
				//dxx = (int)kiked;//xEntityX(kiked->entity,true);
				//dyy = (int)activeSh;//xEntityX(activeSh->entity,true); 
				kiked->vx = (xEntityX(kiked->entity,true)-xEntityX(activeSh->entity,true));
				kiked->vy = (xEntityY(kiked->entity,true)-xEntityY(activeSh->entity,true));
				//kiked->normalyzeMove();
				kiked->normalyzeMove(cos_alpha*activeSh->getRMove()*0.8);
				//dx = kiked->getXMove(); dy = kiked->getYMove();
				//	dx=x1*cos_alpha; dy=y1*cos_alpha;
				
				kiked->activate(true);
				xTranslateEntity(kiked->entity, kiked->vx/4, kiked->vy/4, 0);
				//dxx = kiked->vx; dyy = kiked->vy;
				activeSh->vx = activeSh->vx-kiked->vx;
				activeSh->vy = activeSh->vy-kiked->vy;
			}
		}
	}
	
	// двигаем активные шашки
	bool active = false; // станет true, когда есть активные
	std::vector<Shashka*>::iterator it;
	for (it=shashka_l.begin(); it !=shashka_l.end();it++)
		if ((*it)->flag==1){
			(*it)->stepMove();
			//dxx = (*it)->vx;
			//dyy = (*it)->vy;
			if (fabs(xEntityX((*it)->entity,true)) > 10.2 | fabs(xEntityY((*it)->entity,true)) > 10.2)
				(*it)->activate(false);
			active = true;
		}
	
	if (!active){
	
		//	removeShashka();
		if (gameState != gsWin)
			gameState = gsReadyForTouch;
		changeColor();
		
		return;
	}
}


Shashka* GameStage::getActive(){
	
	std::vector<Shashka*>::iterator itr;
	for (itr=shashka_l.begin(); itr !=shashka_l.end();itr++)
		if ((*itr)->flag !=0) return (*itr);
	return NULL;
}

void GameStage::playAI()
{
	if (gameState != gsWorkAI) return;
	//if (getActive() !=NULL) return;
	std::vector<Shashka*> all_white, all_black;
	std::vector<Shashka*>::iterator it;
	// выбираем все белые шашки
	for (it = shashka_l.begin(); it != shashka_l.end(); it++)
		if ((*it)->stype == sht_white)
			all_white.push_back(*it);
		else
			all_black.push_back(*it);
	// сортируем по расстоянию от края
	std::vector<Shashka*> sorted_white;
	while (!all_white.empty())
	{
		float d = 1000;
		Shashka* m_sh = NULL;
		
		//vector<Shashka*>::iterator itm;
		for (it=all_white.begin(); it != all_white.end(); it++)
		{
			float dx = 10-fabs(xEntityX((*it)->entity,true));
			if (dx < d){d = dx; m_sh = *it;}
			
			float dy = 10-fabs(xEntityY((*it)->entity, true));
			if (dy < d){d = dy; m_sh = *it;}
		}
		sorted_white.push_back(m_sh);
		if(m_sh !=NULL)
			for (it=all_white.begin(); it != all_white.end(); it++)
				if (*it==m_sh) {all_white.erase(it); break;}
	}
	//	float c = 0;
	//	sorted_white = all_white;
	//	std::vector<Shashka*>::iterator i;
	//for (it=sorted_white.begin(); it != sorted_white.end(); it++, c+=30)
	//		xEntityColor((*it)->entity,0, 0, c);
	
	//пара активных шашек
	Shashka* white, *black;
	
	//for (it = sorted_white.begin(); it != sorted_white.end(); it++)
	//{
    it = sorted_white.begin();
		white = *it; black = NULL;
		float d = 1000;
		std::vector<Shashka*>::iterator itb;
		for (itb=all_black.begin(); itb != all_black.end(); itb++)
			if ((*itb)->getDistance(white) < d)
			{
				d = (*itb)->getDistance(white);
				black = *itb;
			}
		
	//	break;//пока для первой попавшейся
	//}
	black->vx = (-xEntityX(black->entity,true) + xEntityX(white->entity,true));
	black->vy = (-xEntityY(black->entity,true) + xEntityY(white->entity,true));
	if (black->getRMove() < 1)
	{
		black->vx = black->vx*2;
		black->vy = black->vy*2;
	}
	else black->normalyzeMove();
	
	black->activate(true);//black->flag = 1;
	touched = true;
	//activeShashka = black;
}

// показ полученных очков
void GameStage::drawScore(bool init, int cnt, long color)
{
	const int SHOW_TIME = 40;
	static int score_timer=0;
	static int model_s =0;// score100;
	static long s_color=0;
	if (init & (cnt==0)){
		score_timer = 0;
		return;
	}
	if (init & (cnt!=0))
	{
		s_color=color;
		score_timer = SHOW_TIME-1;
		switch(cnt)
		{
			case 1: model_s = 100; break;
			case 2: model_s = 200; break;
			case 3: model_s = 300; break;
			case 4: model_s = 400; break;
		}
	}		
	else
	if (score_timer>0){
		if (model_s==100) {
			xShowEntity(score100);
			xScaleEntity(score100, .002*(SHOW_TIME-score_timer), .002*(SHOW_TIME -score_timer), .02,0);
			xRotateEntity(score100, 0, 0, xEntityRoll(camera, 1), 1);
		}
		if (model_s==200){
			xShowEntity(score200);
			xScaleEntity(score200, .002*(SHOW_TIME-score_timer), .002*(SHOW_TIME -score_timer), .02,0);
			xRotateEntity(score200, 0, 0, xEntityRoll(camera, 1), 1);
		}
		if (model_s==300){
			xShowEntity(exellent);
			xScaleEntity(exellent, .002*(SHOW_TIME-score_timer), .002*(SHOW_TIME -score_timer), .02,0);
			xRotateEntity(exellent, 0, 0, xEntityRoll(camera, 1), 1);
		}
		if (model_s==400){
			xShowEntity(quad);
			xScaleEntity(quad, .002*(SHOW_TIME-score_timer), .002*(SHOW_TIME -score_timer), .02,0);
			xRotateEntity(quad, 0, 0, xEntityRoll(camera, 1), 1);
		}
		score_timer--;
	}
	else {
		xHideEntity(score100);
		xHideEntity(score200);
		xHideEntity(exellent);
		xHideEntity(quad);
		cnt=0;
	}
}


void GameStage::removeShashka()
{
//	static float x,y;
//	static int score_timer;
	
	std::vector<Shashka*>::iterator it = shashka_l.begin();
	while (it != shashka_l.end())
	{
		Shashka* sh = *it;
		if (fabs(xEntityX(sh->entity,true)) > 10.2 | fabs(xEntityY(sh->entity,true)) > 10.2)
		{
		//	x=xEntityX(sh->entity,true);
		//	y=xEntityY(sh->entity, true);
		//	score_timer=1;
			if (sh->stype == sht_black) cnt_remove_black++;
			if (sh->stype == sht_white) cnt_remove_white++;			
			
			//if (sh == activeShashka) activeShashka = NULL;
			it = shashka_l.erase(it);			
			delete sh;
			sh = NULL;
		}
		else it++;
	}
	int bc = 0, wc = 0;
	for (it = shashka_l.begin(); it != shashka_l.end(); it++)
		if ((*it)->stype == sht_black) bc++; else wc++;

	
	lastGameBlackCount = bc;
	lastGameWhiteCount = wc;
    
    if (gameState!=gsWin){
        if (bc == 0 | wc == 0)
        {
            gameState = gsWin;
            //return;
        }
    }
    
    /*
	if (gameState!=gsWin){
		// ничья
		if (bc == 0 & wc == 0)
		{
			gameState = gsWin;
			return;
		}
		//белые победили
		if (bc == 0)
		{
			whiteVictoryCount++;
			gameState = gsWin;

			whiteStartLine++;
		}
		//черные победили
		if (wc == 0)
		{
			blackVictoryCount++;
			gameState = gsWin;

		blackStartLine++;
		}
	
		// отодвигаем проигравшего если надо
		if ((bc==0 | wc==0) & (blackStartLine + whiteStartLine > MOVE_POROG[gameProhod]))
		{
			if (bc > wc) whiteStartLine--;
			else blackStartLine--; 
		}
	}
    */
}

void GameStage::changeColor()
{
	if (activeColor == sht_black)
	{
		if (cnt_remove_white==1)	scoreBlack += 1;
		if (cnt_remove_white==2)	scoreBlack += 3;
		if (cnt_remove_white==3)	scoreBlack += 5;
		if (cnt_remove_white>3)		scoreBlack += 10;
		
		drawScore(true, cnt_remove_white,sht_black);
		if(gameState != gsWin) activeColor = sht_white;
	}
	else
	{
		//scoreWhite += cnt_remove_black;
		if (cnt_remove_black==1)	scoreWhite += 1;
		if (cnt_remove_black==2)	scoreWhite += 3;
		if (cnt_remove_black==3)	scoreWhite += 5;
		if (cnt_remove_black>3)		scoreWhite += 10;
		
		drawScore(true, cnt_remove_black,sht_white);
		if (gameState != gsWin){
			activeColor = sht_black;
			gameState = gsWorkAI;
			AI_timer=25;
		}
	}

	cnt_remove_white=0;
	cnt_remove_black=0;

	prevBlack = lastGameBlackCount;
	prevWhite = lastGameWhiteCount;
	//activeShashka = NULL;
}

void GameStage::exitGame()
{
    //global_pause = true;

   // [controller goToGameScore];
    
    return;
}
